package com.cstz.ydshop.common.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.commons.lang.time.DateFormatUtils;

public class DateUtil {
	public static final String TIME_ZONE = "GMT+8";
	public static final String PATTERN_yyyy_MM_dd = "yyyy-MM-dd";
	public static final String PATTERN_yyyyMMdd = "yyyyMMdd";
	public static final String PATTERN_MMdd = "MM-dd";
	public static final String PATTERN_yyMMdd = "yy/MM/dd";
	public static final String PATTERN_yyyyMMddHHmmss = "yyyyMMddHHmmss";
	public static final String PATTERN_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
	public static final String PATTERN_yyMMddHHmmSSss = "yyMMddHHmmSSss";
	public static final String PATTERN_yyyyMMdd_HHmmss = "yyyyMMdd HH:mm:ss";
	public static final String PATTERN_yyyyDotMMDotdd_HHmmss = "yyyy.MM.dd HH:mm:ss";
	public static final String PATTERN_ddMMyyyy_HHmmssDotSSS = "ddMMyyyy HHmmss.SSS";
	public static final String PATTERN_HH_mm_ss = "HH:mm:ss";
	public static final String PATTERN_HHmmss = "HHmmss";
	public static final String PATTERN_yyyy = "yyyy";
	public static final String PATTERN_MM = "MM";
	public static final String PATTERN_dd = "dd";
	public static final String PATTERN_HH = "HH";
	public static final String PATTERN_mm = "mm";
	public static final String PATTERN_ss = "ss";

	public static final long DAY_IN_MILLISECOND = 24 * 60 * 60 * 1000;



	public static Date getPreMonthDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int month = calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, month - 1);
		return calendar.getTime();
	}

	public static Date getPreYearDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		calendar.set(Calendar.YEAR, year - 1);
		return calendar.getTime();
	}

	public static int get(Date date, int field) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.get(field);
	}

	/**
	 *
	 * @Title: parseDate
	 * @Description: 日期转换：String转Date
	 * @param
	 * @return Date
	 * @throws
	 */
	public static Date parseDate(String dateString, String pattern) {
		try {
			if (dateString == null || dateString.length() <= 0) {
				return null;
			}
			return org.apache.commons.lang.time.DateUtils.parseDate(dateString, new String[] { pattern });
		} catch (ParseException e) {
			return null;
		}

	}

	/**
	 *
	 * @Title: parseDate
	 * @Description: 日期转换：String转Date
	 * @param
	 * @return Date
	 * @throws
	 */
	public static Date parseDate(String dateString, String[] patterns) {
		try {
			if (dateString == null || dateString.length() <= 0) {
				return null;
			}
			return org.apache.commons.lang.time.DateUtils.parseDate(dateString, patterns);
		} catch (ParseException e) {
			return null;
		}

	}

	/**
	 *
	 * @Title: parseDateToTimestamp
	 * @Description: 日期转换：Date转Timestamp
	 * @param
	 * @return Timestamp
	 * @throws
	 */
	public static Timestamp parseDateToTimestamp(String dateString) {
		if (dateString == null || "".equals(dateString))
			return null;
		try {
			Date date = parseDate(dateString, PATTERN_yyyy_MM_dd);
			java.sql.Timestamp dateTime = new java.sql.Timestamp(date.getTime());
			return dateTime;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 *
	 * @Title: parseDateToTimestamp
	 * @Description: 日期转换：String转Timestamp
	 * @param
	 * @return Timestamp
	 * @throws
	 */
	public static Timestamp parseDateToTimestamp(String dateString, String formatString) {
		if (dateString == null || "".equals(dateString))
			return null;
		try {
			Date date = parseDate(dateString, formatString);
			java.sql.Timestamp dateTime = new java.sql.Timestamp(date.getTime());
			return dateTime;
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 *
	 * @Title: newDate
	 * @Description: 日期格式转换
	 * @param
	 * @return Date
	 * @throws
	 */
	public static Date newDate(int year, int month, int date) {
		return parseDate("" + year + month + date, month >= 10 ? "yyyyMMdd" : "yyyyMdd");
	}

	/**
	 *
	 * @param year
	 * @param month
	 * @param date
	 * @return
	 */
	public static String format(int year, int month, int date, String pattern) {
		return DateFormatUtils.format(newDate(year, month, date), pattern);
	}

	/**
	 *
	 * @param date
	 * @return
	 */
	public static String format(Date date, String pattern) {
		if (date == null || date.getTime() <= 0) {
			return null;
		}
		return DateFormatUtils.format(date, pattern);
	}

	/**
	 * compare the two dates, and return the subtraction between d1 and d2(d1 -
	 * d2) result > 0 when d1 > d2 and result < 0 when d1 < d2
	 *
	 * @param d1
	 *            Date
	 * @param d2
	 *            Date
	 * @return int
	 */
	public static int compareDateOnDay(Date d1, Date d2) {
		if (d1.getTime() == d2.getTime())
			return 0;
		d1 = org.apache.commons.lang.time.DateUtils.truncate(d1, Calendar.DATE);
		d2 = org.apache.commons.lang.time.DateUtils.truncate(d2, Calendar.DATE);
		long l1 = d1.getTime();
		long l2 = d2.getTime();
		System.out.println(((l1 - l2) / DAY_IN_MILLISECOND));
		return (int) ((l1 - l2) / DAY_IN_MILLISECOND);
	}

	/**
	 * compare the two dates, and return the subtraction between the dates'
	 * month always return > 0
	 *
	 * @param d1
	 *            Date
	 * @param d2
	 *            Date
	 * @return int
	 */
	public static int compareDateOnMonth(Date d1, Date d2) {
		if (d1.getTime() == d2.getTime()) {
			return 0;
		}
		int flag = -1;
		// compare the dates, and put the smaller before
		if (d1.getTime() > d2.getTime()) {
			Date temp = d1;
			d1 = d2;
			d2 = temp;
			flag = 1;
		}
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(d1);
		c2.setTime(d2);
		int y1 = c1.get(Calendar.YEAR);
		int y2 = c2.get(Calendar.YEAR);
		int month1 = c1.get(Calendar.MONTH);
		int month2 = c2.get(Calendar.MONTH);
		int months = 0;
		if (y1 == y2) {
			months = month2 - month1;
		} else {
			months = (y2 - y1 - 1) * 12 + (12 - month1) + month2;
		}
		return months * flag;
	}



	/**
	 * judge the year whether is leap year
	 *
	 * @param year
	 *            int year
	 * @return boolean
	 */
	public static boolean isLeapYear(int year) {
		if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
			return true;
		}
		return false;

	}

	/**
	 * return the days of the year gevin
	 *
	 * @param year
	 *            int year
	 * @return int
	 */
	public static int getYearDays(int year) {
		if (isLeapYear(year)) {
			return 366;
		}
		return 365;
	}

	/**
	 * judge whether the two dates are in same day
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameDay(Date date1, Date date2) {
		return org.apache.commons.lang.time.DateUtils.isSameDay(date1, date2);
	}

	public static Date truncate(Date d, int field) {
		return org.apache.commons.lang.time.DateUtils.truncate(d, field);
	}


	public static boolean isFirstDayOfMonth(Date date) {
		return get(date, Calendar.DAY_OF_MONTH) == 1;
	}

	public static Date getLastMonthDay(int year, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}

	public static Date getLastMonthDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}

	public static Date getFirstMonthDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}

	public static Date getFirstMonthDay(int year, int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month - 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime();
	}



	public static int getDayOfWeek(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.SUNDAY);
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	public static int getWeek(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.WEEK_OF_YEAR);
	}

	public static int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1;
	}

	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}

	public static int getInterval(Date d1, Date d2) {
		int betweenDays = 0;
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(d1);
		c2.setTime(d2);
		if (c1.after(c2)) {
			c1 = c2;
			c2.setTime(d1);
		}
		int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
		betweenDays = c2.get(Calendar.DAY_OF_YEAR) - c1.get(Calendar.DAY_OF_YEAR);
		for (int i = 0; i < betweenYears; i++) {
			c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
			betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
		}
		return betweenDays;
	}



	public static int getMonthInterval(Date startDate, Date endDate) {
		int betweenMonths = 0;
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(startDate);
		c2.setTime(endDate);
		if (c1.after(c2)) {
			c1 = c2;
			c2.setTime(startDate);
		}
		int y1 = c1.get(Calendar.YEAR);
		int y2 = c2.get(Calendar.YEAR);

		int m1 = c1.get(Calendar.MONTH);
		int m2 = c2.get(Calendar.MONTH);

		if (y2 > y1) {
			betweenMonths += (y2 - y1) * 12;
		}
		betweenMonths += (m2 - m1);

		return betweenMonths;
	}

	public static int getWeekInterval(Date startDate, Date endDate) {
		int betweenWeeks = 0;
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();

		c1.setTime(startDate);
		c2.setTime(endDate);
		if (c1.after(c2)) {
			c1 = c2;
			c2.setTime(startDate);
		}

		int y1 = c1.get(Calendar.YEAR);
		int y2 = c2.get(Calendar.YEAR);

		int w1 = c1.get(Calendar.WEEK_OF_YEAR);
		int w2 = c2.get(Calendar.WEEK_OF_YEAR);

		betweenWeeks += (w2 - w1);
		int betweenYears = y2 - y1;
		for (int i = 0; i < betweenYears; i++) {
			c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
			betweenWeeks += c1.getMaximum(Calendar.WEEK_OF_YEAR);
		}

		return betweenWeeks;
	}

	public static int getDaysBetween(java.util.Calendar d1, java.util.Calendar d2) {
		if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
			java.util.Calendar swap = d1;
			d1 = d2;
			d2 = swap;
		}
		int days = d2.get(java.util.Calendar.DAY_OF_YEAR)
				- d1.get(java.util.Calendar.DAY_OF_YEAR);
		int y2 = d2.get(java.util.Calendar.YEAR);
		if (d1.get(java.util.Calendar.YEAR) != y2) {
			d1 = (java.util.Calendar) d1.clone();
			do {
				days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
				d1.add(java.util.Calendar.YEAR, 1);
			} while (d1.get(java.util.Calendar.YEAR) != y2);
		}
		return days;
	}

	public static int getFrameFromString(String hhmmssStringduration) {
		int frame = 0;
		String[] str = hhmmssStringduration.split(":");
		// hh:MM:ss:FF
		Calendar cal = Calendar.getInstance();
		Long start = cal.getTime().getTime();
		cal.add(Calendar.HOUR, Integer.valueOf(str[0]));
		cal.add(Calendar.MINUTE, Integer.valueOf(str[1]));
		cal.add(Calendar.SECOND, Integer.valueOf(str[2]));
		Long end = cal.getTime().getTime();
		frame = ((end.intValue() - start.intValue()) / 1000) * 25 + Integer.valueOf(str[3]).intValue();
		return frame;
	}

	public static String getEndTiemFormDuartion(String playDate, String startTime, Long duration) {
		String[] str = startTime.split(":");

		for (int i = 0; i < str.length; i++) {
			String s = str[i];
			if (s.length() == 1) {
				s = "0" + s;
			}
			str[i] = s;
		}
		String tmpStartDate = str[0] + ":" + str[1] + ":" + str[2];

		long hour = duration / 90000;
		duration = duration % 90000;

		long min = duration / 1500;
		duration = duration % 1500;

		long sec = duration / 25;
		duration = duration % 25;

		Calendar cal = Calendar.getInstance();
		Date dt = DateUtil.parseDate(playDate + " " + tmpStartDate, DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss);
		cal.setTime(dt);
		cal.add(Calendar.HOUR, Integer.valueOf(String.valueOf(hour)));
		cal.add(Calendar.MINUTE, Integer.valueOf(String.valueOf(min)));
		cal.add(Calendar.SECOND, Integer.valueOf(String.valueOf(sec)));
		String tmp = DateUtil.format(cal.getTime(), DateUtil.PATTERN_HH_mm_ss);
		tmp = tmp + ":" + duration;
		return tmp;
	}

	/**
	 *
	 * @return
	 */
	public static Date getBeginingDate() {
		return parseDate("1970-01-01 00:00:00", PATTERN_yyyy_MM_dd_HH_mm_ss);
	}

	public static String getHHmmssStringFormDuartion(Long duration) {
		long hour = duration / 90000;
		duration = duration % 90000;

		long min = duration / 1500;
		duration = duration % 1500;

		long sec = duration / 25;
		duration = duration % 25;

		String dur = ":0";
		if (duration.intValue() < 10) {
			dur = dur + duration;
		} else {
			dur = ":" + duration;
		}

		String tmp = "";

		if (Integer.valueOf(String.valueOf(hour)).intValue() < 10) {
			tmp += "0" + hour;
		} else {
			tmp += hour;
		}
		tmp += ":";

		if (Integer.valueOf(String.valueOf(min)).intValue() < 10) {
			tmp += "0" + min;
		} else {
			tmp += min;
		}
		tmp += ":";

		if (Integer.valueOf(String.valueOf(sec)).intValue() < 10) {
			tmp += "0" + sec;
		} else {
			tmp += sec;
		}
		tmp += dur;
		return tmp;
	}

	public static String getEndTimeFormDuartion(String playDate, String startTime, Long duration) {
		String[] str = startTime.split(":");

		for (int i = 0; i < str.length; i++) {
			String s = str[i];
			if (s.length() == 1) {
				s = "0" + s;
			}
			str[i] = s;
		}
		String tmpStartDate = str[0] + ":" + str[1] + ":" + str[2];

		long hour = duration / 90000;
		duration = duration % 90000;

		long min = duration / 1500;
		duration = duration % 1500;

		long sec = duration / 25;
		duration = duration % 25;

		Calendar cal = Calendar.getInstance();
		Date dt = DateUtil.parseDate(playDate + " " + tmpStartDate, DateUtil.PATTERN_yyyyMMdd_HHmmss);
		cal.setTime(dt);
		cal.add(Calendar.HOUR, Integer.valueOf(String.valueOf(hour)));
		cal.add(Calendar.MINUTE, Integer.valueOf(String.valueOf(min)));
		cal.add(Calendar.SECOND, Integer.valueOf(String.valueOf(sec)));
		String tmp = DateUtil.format(cal.getTime(), DateUtil.PATTERN_yyyyMMddHHmmss);
		return tmp;
	}

	public static String getHHmmssStringFormDuartionNoFrame(Long duration) {
		long hour = duration / 90000;
		duration = duration % 90000;

		long min = duration / 1500;
		duration = duration % 1500;

		long sec = duration / 25;
		duration = duration % 25;

		String tmp = "";

		if (Integer.valueOf(String.valueOf(hour)).intValue() < 10) {
			tmp += "0" + hour;
		} else {
			tmp += hour;
		}

		if (Integer.valueOf(String.valueOf(min)).intValue() < 10) {
			tmp += "0" + min;
		} else {
			tmp += min;
		}

		if (Integer.valueOf(String.valueOf(sec)).intValue() < 10) {
			tmp += "0" + sec;
		} else {
			tmp += sec;
		}
		return tmp;
	}

	public static String getCurrentDateTime(String formatString) {
		SimpleDateFormat format = new SimpleDateFormat(formatString);
		format.setTimeZone(TimeZone.getTimeZone(TIME_ZONE));
		Date nowDate = new Date();
		return format.format(nowDate);
	}

	public static String getCurrentDate() {
		return getCurrentDateTime(PATTERN_yyyy_MM_dd);
	}

	public static String getCurrentDateTime() {
		return getCurrentDateTime(PATTERN_yyyy_MM_dd_HH_mm_ss);
	}

	public static Date getCurrentDateTimeToDate() {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_yyyy_MM_dd_HH_mm_ss);
			return dateFormat.parse(getCurrentDateTime());
		} catch (ParseException e) {
			return null;
		}
	}

	public static Date getCurrentDateToDate() {
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(PATTERN_yyyy_MM_dd);
			return dateFormat.parse(getCurrentDate());
		} catch (ParseException e) {
			return null;
		}
	}

	public static Timestamp getCurrentDateTimeToTimestamp() {
		try {
			return new Timestamp(getCurrentDateTimeToDate().getTime());
		} catch (Exception e) {
			return null;
		}
	}

	public static Timestamp getCurrentDateToTimestamp() {
		try {
			return new Timestamp(getCurrentDateToDate().getTime());
		} catch (Exception e) {
			return null;
		}
	}

	public static String format(Timestamp time) {
		return format(time, PATTERN_yyyy_MM_dd_HH_mm_ss);
	}

	public static String format(Timestamp time, String pattern) {
		if (time == null || time.getTime() <= 0) {
			return "";
		}
		if (pattern == null || pattern.trim().length() <= 0) {
			pattern = PATTERN_yyyy_MM_dd_HH_mm_ss;
		}
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date date = new Date(time.getTime());
		return format.format(date);
	}

	public static boolean isToday(Date date) {
		Calendar now = Calendar.getInstance();
		Calendar c = Calendar.getInstance();
		c.setTime(date);

		if ((now.get(Calendar.YEAR) == c.get(Calendar.YEAR))
				&& (now.get(Calendar.MONTH) == c.get(Calendar.MONTH))
				&& (now.get(Calendar.DATE) == c.get(Calendar.DATE))) {
			return true;
		}

		return false;
	}

	public static String getLastMonthFirstDay() {
		return getMonthFirstDay(-1);
	}

	public static String getNextMonthFirstDay() {
		return getMonthFirstDay(1);
	}

	public static String getMonthFirstDay(int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeZone(TimeZone.getTimeZone(TIME_ZONE));
		calendar.set(Calendar.DATE, 1);// ��Ϊ��ǰ�µ�1��
		calendar.add(Calendar.MONTH, month);
		return format(calendar.getTime(), PATTERN_yyyy_MM_dd) + " 00:00:00";
	}

	public static String getLastMonthLastDay() {
		return getMonthLastDay(-1);
	}

	public static String getNextMonthLastDay() {
		return getMonthLastDay(1);
	}

	public static String getMonthLastDay(int month) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeZone(TimeZone.getTimeZone(TIME_ZONE));
		calendar.set(Calendar.DATE, 1);
		calendar.add(Calendar.MONTH, month + 1);
		calendar.add(Calendar.DATE, -1);
		return format(calendar.getTime(), PATTERN_yyyy_MM_dd) + " 23:59:59";
	}

	public static int dateDiff(String s, Timestamp timestamp,
			Timestamp timestamp1) {

		if (timestamp == null || timestamp1 == null)
			return -1;
		Date date = null;
		Date date1 = null;
		date = new Date(timestamp.getTime());
		date1 = new Date(timestamp1.getTime());
		Calendar calendar = null;
		Calendar calendar1 = null;
		calendar = Calendar.getInstance();
		calendar1 = Calendar.getInstance();
		calendar.setTime(date);
		long l = date.getTime() + (long) calendar.get(15)
				+ (long) calendar.get(16);
		calendar1.setTime(date1);
		long l1 = date1.getTime() + (long) calendar1.get(15)
				+ (long) calendar1.get(16);
		int i = (int) (l / 0x36ee80L);
		int j = (int) (l1 / 0x36ee80L);
		int k = i / 24;
		int i1 = j / 24;
		int j1 = i1 - k;
		int k1 = calendar1.get(7) - calendar.get(7) >= 0 ? 0 : 1;
		int i2 = j1 / 7 + k1;
		int j2 = calendar1.get(1) - calendar.get(1);
		int k2 = (j2 * 12 + calendar1.get(2)) - calendar.get(2);

		long s1 = date.getTime();
		long s2 = date1.getTime();

		long diffS = s2 - s1;
		int diff1 = (int) (diffS / 1000 / 60);

		if ("YEAR".equals(s))
			return j2;
		if ("MONTH".equals(s))
			return k2;
		if ("DATE".equals(s))
			return j1;
		if ("WEEK".equals(s))
			return i2;
		if ("SECOND".equals(s))
			return (int)(diffS / 1000);
		if ("MINUTE".equals(s)) {
			return diff1;
		} else
			return j1;
	}

	public static void main(String[] args) {
		Date date = new Date();

		String cnTime = DateUtil.format(date, "yyyy年MM月dd日, HH:mm:ss");
		String enTime = DateUtil.format(date, "yyyy年MM月dd日, HH:mm:ss");

		enTime=DateFormatUtils.format(date, "dd MMMM yyyy, HH:mm:ss", Locale.US);
		System.out.println(cnTime);
		System.out.println(enTime);
		
		
	}

}